package com.ttg.common.utils;


import com.ttg.common.entities.UserTokenInfo;
import com.ttg.common.exception.BaseException;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
 * @author ZQ
 * @Description
 * @date 2020/12/8 11:19
 * @version 1.0
 */
@Slf4j
public class UserUtil {
    public static String KEY_USERINFO_IN_HTTP_HEADER = "TLINX-USERINFO-HEAD";
    private static ThreadLocal<UserTokenInfo> threadLocal = new ThreadLocal<>();


    public static UserTokenInfo getUser() {
        return threadLocal.get();
    }

    public static UserTokenInfo getUserThrow() {
        return Optional.ofNullable(getUser()).orElseThrow(() -> new BaseException("获取授权信息错误"));
    }


    public static void setUser(UserTokenInfo userTokenInfo) {
        threadLocal.set(userTokenInfo);
    }

    public static void removeUser() {
        threadLocal.remove();
    }

}
